$.ajax({
    type: 'get',
    url: '/posts',
    success: function(response) {
        console.log(response);
        var html = template('postsTpl', response);
        $('#postsBox').html(html);
        var pageHtml = template('pageTpl', response);
        $('#page').html(pageHtml);
    }
});

function formDate(date) {
    var date = new Date(date);
    return `${date.getFullYear()}年${date.getMonth() +
        1}月${date.getDate()}日${date.getHours()}时${date.getMinutes()}分${date.getSeconds()}秒`;
}

// 分页功能
function changePage(page) {
    $.ajax({
        type: 'get',
        url: '/posts',
        data: {
            page: page
        },
        success: function(response) {
            console.log(response);
            var html = template('postsTpl', response);
            $('#postsBox').html(html);
            var pageHtml = template('pageTpl', response);
            $('#page').html(pageHtml);
        }
    });
}

// 查询分类功能
$.ajax({
    type: 'get',
    url: '/categories',
    success: function(response) {
        var html = template('categoryTpl', { data: response });
        $('#categoryBox').html(html);
    }
});
$('#filterForm').on('submit', function() {
    var data = $(this).serialize();
    $.ajax({
        type: 'get',
        url: '/posts',
        data: data,
        success: function(response) {
            console.log(response);
            var html = template('postsTpl', response);
            $('#postsBox').html(html);
            var pageHtml = template('pageTpl', response);
            $('#page').html(pageHtml);
        }
    });
    return false;
});
